package com.itic.system.order.orderproductitem.service;

import java.text.ParseException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.sys.syssetting.persistence.dao.SysSettingDao;
import com.itic.appbase.applications.sys.user.persistence.model.SysUser;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.CalendarHelper;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.appbase.framework.utils.RandomOrderNumHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.configuration.PropertyHelper;
import com.itic.appbase.framework.xcx.utils.WeixinUtil;
import com.itic.appbase.framework.xcx.vo.WXPayResult;
import com.itic.system.market.product.persistence.dao.ProductDao;
import com.itic.system.market.product.persistence.model.Product;
import com.itic.system.market.productitem.persistence.model.ProductItem;
import com.itic.system.order.orderproduct.persistence.dao.OrderProductDao;
import com.itic.system.order.orderproduct.persistence.model.OrderProduct;
import com.itic.system.order.orderproductitem.persistence.dao.OrderProductItemDao;
import com.itic.system.order.orderproductitem.persistence.model.OrderProductItem;
import com.itic.system.order.orderproductitem.persistence.model.OrderProductItemExample;
import com.itic.system.order.orderproductitem.persistence.model.OrderProductItemExample.Criteria;


@Service
@Transactional
public class OrderProductItemService
     extends CrudService<OrderProductItemDao,OrderProductItem,OrderProductItemExample> {
    @Autowired
    private OrderProductItemDao orderProductItemDao;
    @Autowired
    private OrderProductDao orderProductDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private SysSettingDao sysSettingDao;
    
	public void accountPay(SysUser user, OrderProductItem orderProductItem) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 添加支付
	 * @param user
	 * @param productItem
	 * @return
	 */
	public WXPayResult addOrderAndReturnWxPay(SysUser user, ProductItem productItem) {
		
		Product product = productDao.selectByPrimaryKey(productItem.getProductId());
		
		//创建订单
		Date now = new Date();
		OrderProduct order = new OrderProduct();
		this.initDefalutFields(order);
		order.setUserId(user.getId());
		order.setUserName(user.getUsername());
		order.setExpiredTime(CalendarHelper.getDateAfter(now, 1));
		order.setProductId(productItem.getProductId());
		order.setProductSummary(productItem.getSummary());
		order.setStatus("noPay");
		order.setTotalCount(1);
		order.setTotalMoney(productItem.getFee());
		order.setOrderNum(RandomOrderNumHelper.getOrderNumber());
		
		orderProductDao.insert(order);
		
		//创建订单项
		OrderProductItem orderItem = new OrderProductItem();
		this.initDefalutFields(orderItem);
		
		orderItem.setUserId(user.getId());
		orderItem.setUserName(order.getUserName());
		
		orderItem.setOrderId(order.getId());
		orderItem.setProductId(product.getId());
		orderItem.setProductItemId(productItem.getId());
		orderItem.setProductItemSummary(productItem.getSummary());
		
		orderItem.setTotalCount(1);
		orderItem.setTotalMoney(productItem.getFee());
		
		orderProductItemDao.insert(orderItem);
		String isOpenPay = sysSettingDao.getByKey("IS_OPEN_PAY");
		if( StringHelper.isBlank(isOpenPay) ){
			WXPayResult wxPayResult  = WeixinUtil.wxPay(user.getOpenId(), PropertyHelper.getString("wxpay.xcx.appid"), PropertyHelper.getString("wxpay.xcx.mchid"), PropertyHelper.getString("wxpay.xcx.key"), order.getId(),0.01, "118.190.144.162", "惠发科技，订单号码："+order.getOrderNum(),PropertyHelper.getString("wxpay.xcx.productItem.notifyUrl"));
			return wxPayResult;
		}else{
			WXPayResult wxPayResult  = WeixinUtil.wxPay(user.getOpenId(), PropertyHelper.getString("wxpay.xcx.appid"), PropertyHelper.getString("wxpay.xcx.mchid"), PropertyHelper.getString("wxpay.xcx.key"), order.getId(),order.getTotalMoney(), "118.190.144.162", "惠发科技，订单号码："+order.getOrderNum(),PropertyHelper.getString("wxpay.xcx.productItem.notifyUrl"));
			return wxPayResult;
		}
		
	}

	/**
	 * 支付成功
	 * @param appid
	 * @param out_trade_no
	 * @param finishTime
	 * @param total_fee
	 * @param transaction_id
	 * @param openid
	 * @param mch_id
	 */
	public String paySuccessDealOrder(String appid, String out_trade_no, String finishTime, String total_fee,
			String transaction_id, String openid, String mch_id) {
		OrderProduct orderProduct = orderProductDao.selectByPrimaryKey(out_trade_no);
		if( null != orderProduct ){
			orderProduct.setStatus("success");
			try {
				orderProduct.setPayTime(DateHelper.parseDate(finishTime, "yyyyMMddHHmmss"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			orderProduct.setPayNum(transaction_id);
			orderProductDao.updateByPrimaryKey(orderProduct);
			
			//设置用户订单项商品为成功状态
			OrderProductItemExample example = new OrderProductItemExample();
			Criteria criteria = example.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andOrderIdEqualTo(orderProduct.getId());
			
			OrderProductItem item = new OrderProductItem();
			item.setNote(SUCCESS);
			orderProductItemDao.updateByExampleSelective(item, example);
		}
		return SUCCESS;
	}


}